package com.shujia.stream

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import java.util.Properties
import scala.io.Source

object CarDataToKafka {
  def main(args: Array[String]): Unit = {

    /**
     * 使用java api实时模拟数据生产
     *
     */

    val properties = new Properties()
    //指定kafka broker地址
    properties.setProperty("bootstrap.servers", "master:9092")

    //设置key 和value的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")


    //生产者
    val producer = new KafkaProducer[String, String](properties)


    //读取卡口数据
    val cars: Iterator[String] = Source
      .fromFile("D:\\上课资料\\十七期\\大数据\\项目\\智慧交通\\数据\\过车数据\\cars.json")
      .getLines()


    //循环将卡口过车数据写入kafka
    for (car <- cars) {
      println("正在生产数据")
      val record = new ProducerRecord[String, String]("car", car)
      producer.send(record)
      producer.flush()

      Thread.sleep(20)
    }

  }

}
